package com.wholesale.utils;

import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import com.wholesale.R;
import com.wholesale.beans.ShippingMethod;

public class ShippingMethodListAdapter extends ArrayAdapter<ShippingMethod> {

	// ListView中的数据来源
	private ListView listView;
	private ImageView selectImage;
	private int selected = 0;
	private static HashMap<Integer, Boolean> isSelected = new HashMap<Integer, Boolean>();;

	public ShippingMethodListAdapter(Context context, ListView listView) {
		super(context, 0);
		this.listView = listView;
	}

	public ShippingMethodListAdapter(Context context,
			List<ShippingMethod> shippingMethodList, ListView listView) {
		super(context, 0, shippingMethodList);
		this.listView = listView;
		int size = shippingMethodList.size();
		for (int i = 0; i < size; i++) {
			isSelected.put(i, false);
		}
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Activity activity = (Activity) getContext();

		// Inflate the views from XML
		View rowView = convertView;
		ShippingMethodCache viewCache;
		if (rowView == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			rowView = inflater.inflate(R.layout.ordershipmethoditem, null);
			viewCache = new ShippingMethodCache(rowView);
			rowView.setTag(viewCache);
		} else {
			viewCache = (ShippingMethodCache) rowView.getTag();
		}
		ShippingMethod shippingMethod = getItem(position);
		// Set the text on the TextView
		viewCache.getShippingName().setText(shippingMethod.getShippingName());
		viewCache.getShippingDesc().setText(
				Html.fromHtml(shippingMethod.getShippingDesc()));
		viewCache.getCost().setText(
				"US$ "
						+ Constants.getTwoDecimalFloor(shippingMethod
								.getShippingFee()));
		if (shippingMethod.getInsure().equals("1")) {
			viewCache.getInsure().setText("Insurance included");
		} else {
			viewCache.getInsure().setText("Insurance no included");
		}
		if (selected == position) {
			rowView.findViewById(R.id.selectedBtn).setVisibility(View.VISIBLE);
		} else {
			rowView.findViewById(R.id.selectedBtn)
					.setVisibility(View.INVISIBLE);
		}
		// if(isSelected.get(position)){
		// rowView.findViewById(R.id.selectedBtn).setVisibility(View.VISIBLE);
		// }else{
		// rowView.findViewById(R.id.selectedBtn).setVisibility(View.INVISIBLE);
		// }
		return rowView;
	}

	public void setSelected(int index) {
		this.selected = index;
	}

	public void setSelect(int index, View childView) {
		int size = isSelected.size();
		for (int i = 0; i < size; i++) {
			isSelected.put(i, false);
		}
		isSelected.put(index, true);
		this.selected = index;
		if (selectImage != null) {
			selectImage.setVisibility(View.INVISIBLE);
		}
		this.selectImage = (ImageView) childView.findViewById(R.id.selectedBtn);
		selectImage.setVisibility(View.VISIBLE);
		this.listView.postInvalidate();
	}

	/**
	 * 批量添加数据
	 * 
	 * @param items
	 */
	public void addItems(List<ShippingMethod> items) {
		int size = items.size();
		for (int i = 0; i < size; i++) {
			isSelected.put(i, false);
		}
		for (ShippingMethod shippingMethod : items) {
			this.add(shippingMethod);
		}
	}

}
